

package vinterprojekt.gui;

import java.awt.Color;
import javax.swing.ImageIcon;
import javax.swing.SwingWorker;
import java.util.ArrayList;
import javax.swing.JOptionPane;
import vinterprojekt.controller.*;
import vinterprojekt.entity.Book;
import libisbn.ISBNResult;


/**
 *
 * @author Administrator
 */


public class AppFrame extends javax.swing.JFrame
{
    private Facade facade;
    private TableHelper tableHelper;
    
    private ImageIcon iconOk;
    private ImageIcon iconError;

    
    public AppFrame()
    {
        initComponents();

        try
        {
            setIconImage(javax.imageio.ImageIO.read(getClass().getResource("icon_app.png")));
        }

        catch (Exception e)
        {
            vinterprojekt.util.ErrorHandler.error(e, false);
        }

        facade = Facade.getInstance();
        iconOk = loadIcon("icon_ok.png");
        iconError = loadIcon("icon_error.png");

        checkOnlineButton.setMnemonic('h');
        bookSeachButton.setMnemonic('s');
        bookCreateButton.setMnemonic('o');
        bookUpdateButton.setMnemonic('g');
        clearTextFieldsButton.setMnemonic('r');
        showAllButton.setMnemonic('a');

        tableHelper = new TableHelper(resultTable);

        updateTitle();
    }

    private ImageIcon loadIcon(String path)
    {
        java.net.URL url = getClass().getResource(path);
        return new ImageIcon(url);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        bookTab = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        label1 = new java.awt.Label();
        label2 = new java.awt.Label();
        label3 = new java.awt.Label();
        label4 = new java.awt.Label();
        label5 = new java.awt.Label();
        label6 = new java.awt.Label();
        checkOnlineButton = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        bookCreateButton = new javax.swing.JButton();
        bookSeachButton = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        resultTable = new javax.swing.JTable();
        statusLabel = new javax.swing.JLabel();
        titleTextField = new javax.swing.JTextField();
        authorTextField = new javax.swing.JTextField();
        isbnTextField = new javax.swing.JTextField();
        publisherTextField = new javax.swing.JTextField();
        editionTextField = new javax.swing.JTextField();
        yOPTextField = new javax.swing.JTextField();
        bookUpdateButton = new javax.swing.JButton();
        bookDeleteButton = new javax.swing.JButton();
        clearTextFieldsButton = new javax.swing.JButton();
        showAllButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Organizer");
        setResizable(false);

        label1.setText("ISBN : ");

        label2.setText("Forfatter : ");

        label3.setText("Titel : ");

        label4.setText("Forlag : ");

        label5.setText("Udgave : ");

        label6.setText("Udgivelses år : ");

        checkOnlineButton.setText("Hent online (alt+h)");
        checkOnlineButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                checkOnlineButtonActionPerformed(evt);
            }
        });

        jPanel2.setBorder(null);

        bookCreateButton.setText("Opret (alt+o)");
        bookCreateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bookCreateButtonActionPerformed(evt);
            }
        });

        bookSeachButton.setText("Søg (alt+s)");
        bookSeachButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bookSeachButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(bookSeachButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(bookCreateButton)
                .addContainerGap(247, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bookSeachButton)
                    .addComponent(bookCreateButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        resultTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "ISBN", "Forfatter", "Titel", "Forlag", "Udgave", "Udgivelsesår"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, true, true, true, true, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        resultTable.setColumnSelectionAllowed(true);
        jScrollPane1.setViewportView(resultTable);
        resultTable.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        resultTable.getColumnModel().getColumn(4).setPreferredWidth(6);
        resultTable.getColumnModel().getColumn(5).setPreferredWidth(10);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 705, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 404, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        statusLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/vinterprojekt/gui/icon_ok.png"))); // NOI18N
        statusLabel.setText("Status Ok");

        bookUpdateButton.setText("Gem ændringer (alt+g)");
        bookUpdateButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bookUpdateButtonActionPerformed(evt);
            }
        });

        bookDeleteButton.setText("Slet");
        bookDeleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bookDeleteButtonActionPerformed(evt);
            }
        });

        clearTextFieldsButton.setText("Ryd Felter (alt+r)");
        clearTextFieldsButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                clearTextFieldsButtonActionPerformed(evt);
            }
        });

        showAllButton.setText("Vis alle (alt+a)");
        showAllButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                showAllButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(statusLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 439, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(300, Short.MAX_VALUE))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(bookDeleteButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(bookUpdateButton)
                        .addGap(394, 394, 394)
                        .addComponent(showAllButton)
                        .addContainerGap())
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addContainerGap())
                        .addGroup(jPanel1Layout.createSequentialGroup()
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(label3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(label4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(label5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(label6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addContainerGap())
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(yOPTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 419, Short.MAX_VALUE)
                                        .addComponent(editionTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 419, Short.MAX_VALUE)
                                        .addComponent(publisherTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 419, Short.MAX_VALUE)
                                        .addComponent(authorTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 419, Short.MAX_VALUE)
                                        .addComponent(titleTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 419, Short.MAX_VALUE)
                                        .addComponent(isbnTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 419, Short.MAX_VALUE))
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(checkOnlineButton)
                                    .addGap(55, 55, 55))
                                .addGroup(jPanel1Layout.createSequentialGroup()
                                    .addComponent(clearTextFieldsButton)
                                    .addContainerGap()))))))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(39, 39, 39)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(label1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(isbnTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(checkOnlineButton)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(label2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(authorTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(titleTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(label3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(label4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(publisherTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(label5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(editionTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(label6, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(yOPTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(clearTextFieldsButton)
                .addGap(42, 42, 42)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(bookDeleteButton)
                            .addComponent(bookUpdateButton))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 35, Short.MAX_VALUE)
                        .addComponent(statusLabel))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(showAllButton)
                        .addContainerGap())))
        );

        label6.getAccessibleContext().setAccessibleName("Udgivelsesår : ");

        bookTab.addTab("Bøger", jPanel1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 743, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(bookTab, javax.swing.GroupLayout.DEFAULT_SIZE, 743, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 931, Short.MAX_VALUE)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(bookTab, javax.swing.GroupLayout.DEFAULT_SIZE, 931, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void bookSeachButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bookSeachButtonActionPerformed

        String isbn = isbnTextField.getText().trim();
        String author = authorTextField.getText().trim();
        String title = titleTextField.getText().trim();
        
        /* nedenstående ligner noget David har kastet op - undskyld */

        tableHelper.updateContent(null);

        if (isbn.isEmpty() && author.isEmpty() && title.isEmpty())
        {
            showError("Søg på en kombination af ISBN, forfatter eller titel");
            return;
        }

        boolean searchIsbn = false;
        boolean searchAttribs = false;

        if (!isbn.isEmpty() && ! facade.getInputValidator().validateISBN(isbn))
        {
            showError("Invalid ISBN angivet");
            return;
        }

        searchIsbn = !isbn.isEmpty();
        
        if (!author.isEmpty() || !title.isEmpty())
        {
            if (!facade.getInputValidator().validateSearch(author, title))
            {
                showError("Invalide søgeparametre - ISBN, titel samt forfatter er tomme");
                return;
            }

            else searchAttribs = true;
        }
        
        ArrayList<Book> results = new ArrayList<Book>();
        if (searchIsbn)
        {
            Book book = facade.getBookController().bookSearchIsbn(isbn);
            if (book != null) results.add(book);
        }

        if (searchAttribs)
        {
            Book[] books = facade.getBookController().bookSearch(title, author);
            for (Book book : books) results.add(book);
        }

        if (results.size() > 0)
        {
            tableHelper.updateContent(results.toArray(new Book[0]));
            String msg = String.format("Fandt %d %s", new Integer(results.size()), results.size() == 1 ? "bog" : "bøger");
            showSucces(msg);
        }

        else
        {
            showError("Ingen bøger fundet");
        }
       
    }//GEN-LAST:event_bookSeachButtonActionPerformed

    private void bookCreateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bookCreateButtonActionPerformed

        String title = titleTextField.getText().trim();
        String yearOfPublication = yOPTextField.getText().trim();
        String edition = editionTextField.getText().trim();
        String publisher = publisherTextField.getText().trim();
        String isbn = isbnTextField.getText().trim();
        String isbnSuffix = "11"; // ??
        String remark = "En kort bemærkning";
        String author = authorTextField.getText().trim();
        boolean avail = true; // ??

        if (title.isEmpty())
        {
            showError("Titel er tom");
            return;
        }
        
        if (yearOfPublication.isEmpty())
        {
            showError("Udgivelsesår er tom");
            return;
        }

        if (edition.isEmpty())
        {
            showError("Udgave er tom");
            return;
        }

        if (publisher.isEmpty())
        {
            showError("Udgiver er tom");
            return;
        }

        if (!facade.getInputValidator().validateISBN(isbn))
        {
            showError("Invalid ISBN");
            return;
        }

        // valider ikke ISBNSuffix
        // valider ikke remark

        if (author.isEmpty())
        {
            showError("Forfatter er tom");
            return;
        }

        if (!facade.getInputValidator().validateCreate(title, yearOfPublication, edition, publisher, isbn, isbnSuffix, remark, author, avail))
        {
            // uhh - præciser!
            showError("Invalid input");
            return;
        }

        else
        {
            Book newBook = facade.getBookController().bookNewInstance(title, yearOfPublication, edition, publisher, isbn, isbnSuffix, remark, author, avail);
            
            if (facade.getBookController().bookSave(newBook))
            {
                updateTitle();
                showSucces("Bog oprettet");
            }

            else
            {
                showError("ISBN findes allerede i databasen");
            }
          
        }
        
    }//GEN-LAST:event_bookCreateButtonActionPerformed

    private void bookUpdateButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bookUpdateButtonActionPerformed

       String isbn = tableHelper.getSelectedIsbn();

       if (isbn == null)
       {
           showError("Marker først en række");
       }

       else
       {
           String author = tableHelper.getSelectedAuthor();
           String title = tableHelper.getSelectedTitle();
           String publisher = tableHelper.getSelectedPublisher();
           String edition = tableHelper.getSelectedEdition();
           String yop = tableHelper.getSelectedYearOfPublication();

           if (author.isEmpty())
           {
               showError("Forfatter er tom");
               return;
           }

           if (title.isEmpty())
           {
               showError("Titel er tom");
               return;
           }

           if (publisher.isEmpty())
           {
               showError("Udgiver er tom");
               return;
           }

           if (edition.isEmpty())
           {
               showError("Udgave er tom");
               return;
           }

           if (yop.isEmpty())
           {
               showError("Udgivelsesår er tom");
               return;
           }

           else
           {
               Book orig = facade.getBookController().bookSearchIsbn(isbn);
               facade.getBookController().bookUpdate(orig, title, edition, author, publisher, yop);

               showSucces("Bog opdateret");
           }
           
       }
    }//GEN-LAST:event_bookUpdateButtonActionPerformed

    private void bookDeleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bookDeleteButtonActionPerformed

        String isbn = tableHelper.getSelectedIsbn();

        if (isbn == null)
        {
            showError("Marker først en række");
            return;
        }

        else
        {
            String msg = String.format("Vil du slette %s permanent?\n(ISBN %s)", tableHelper.getSelectedTitle(), isbn);
            int choice = JOptionPane.showConfirmDialog(this, msg, "Slet bog permanent?", JOptionPane.YES_NO_OPTION);

            if (choice == JOptionPane.YES_OPTION)
            {
                Book book = facade.getBookController().bookSearchIsbn(isbn);
                facade.getBookController().bookDelete(book);
                tableHelper.updateContent(null);

                updateTitle();
                showSucces("Bog slettet permanent");
            }

            else showSucces("Bog alligevel ikke slettet");
        }
    }//GEN-LAST:event_bookDeleteButtonActionPerformed

    private void checkOnlineButtonActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_checkOnlineButtonActionPerformed
    {//GEN-HEADEREND:event_checkOnlineButtonActionPerformed
        final String isbn = isbnTextField.getText().trim();

        if (isbn.isEmpty())
        {
            showError("ISBN-feltet er tomt");
            return;
        }

        if (facade.getInputValidator().validateISBN(isbn))
        {
            SwingWorker worker = new SwingWorker<ISBNResult, Void>()
            {

                @Override
                protected ISBNResult doInBackground() throws Exception
                {
                    showSucces("Arbejder ...");
                    return facade.isbnLookup(isbn);
                }

                /* done() kører på swing-tråden */
                @Override
                public void done()
                {
                    try
                    {
                        ISBNResult result = get();

                        String title = result.getTitle();
                        String publisher = result.getPublisher();
                        String author = result.getAuthor();

                        authorTextField.setText(author);
                        titleTextField.setText (title);
                        publisherTextField.setText(publisher);

                        showSucces("ISBN fundet");
                    }

                    catch (Exception e)
                    {
                        showError("ISBN ikke fundet");
                    }
                }

            };

            worker.execute();
        }
    }//GEN-LAST:event_checkOnlineButtonActionPerformed

    private void clearTextFieldsButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_clearTextFieldsButtonActionPerformed
        clearTextFields();
    }//GEN-LAST:event_clearTextFieldsButtonActionPerformed

    private void showAllButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_showAllButtonActionPerformed

        Book[] all = facade.getBookController().BookGetAll();
        if (all.length == 0)
        {
            tableHelper.updateContent(null);
            showSucces("Ingen bøger fundet");
        }

        else
        {
            tableHelper.updateContent(all);
            showSucces("Viser " + all.length + " bøger");
        }
    }//GEN-LAST:event_showAllButtonActionPerformed

    private void clearTextFields()
    {
        isbnTextField.setText("");
        authorTextField.setText("");
        titleTextField.setText("");
        publisherTextField.setText("");
        editionTextField.setText("");
        yOPTextField.setText("");

        tableHelper.updateContent(null);
        showSucces("Status Ok");
    }

    private void showError(String error)
    {
        statusLabel.setText(error);
        statusLabel.setForeground(Color.red);
        statusLabel.setIcon(iconError);
    }

    private void showSucces(String succes)
    {
        statusLabel.setForeground(Color.black);
        statusLabel.setText(succes);
        statusLabel.setIcon(iconOk);
    }

    private void updateTitle()
    {
        int count = facade.getBookController().bookGetTotalCount();
       
        String title = String.format("Organizer - %d %s", new Integer(count), (count == 0 || count > 1) ? "bøger" : "bog");
        setTitle(title);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField authorTextField;
    private javax.swing.JButton bookCreateButton;
    private javax.swing.JButton bookDeleteButton;
    private javax.swing.JButton bookSeachButton;
    private javax.swing.JTabbedPane bookTab;
    private javax.swing.JButton bookUpdateButton;
    private javax.swing.JButton checkOnlineButton;
    private javax.swing.JButton clearTextFieldsButton;
    private javax.swing.JTextField editionTextField;
    private javax.swing.JTextField isbnTextField;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private java.awt.Label label1;
    private java.awt.Label label2;
    private java.awt.Label label3;
    private java.awt.Label label4;
    private java.awt.Label label5;
    private java.awt.Label label6;
    private javax.swing.JTextField publisherTextField;
    private javax.swing.JTable resultTable;
    private javax.swing.JButton showAllButton;
    private javax.swing.JLabel statusLabel;
    private javax.swing.JTextField titleTextField;
    private javax.swing.JTextField yOPTextField;
    // End of variables declaration//GEN-END:variables


   

}
